


//
//  LeeShippingAddressViewController.m
//  firstpoint
//
//  Created by 李雪虎 on 2017/7/10.
//  Copyright © 2017年 xzcysoft. All rights reserved.
//

#import "LeeShippingAddressViewController.h"
#import "LeeShippingAddressTableViewCell.h"

@interface LeeShippingAddressViewController ()<UITableViewDelegate,UITableViewDataSource>

{
    NSArray*dataArr;
    NSMutableArray*textFieldArr;
}
@property(nonatomic,strong)UITableView *FavoritesTabelView;
@property(nonatomic,strong)UIView *bigView;
@end

@implementation LeeShippingAddressViewController
-(void)chaxunList//查询
{
    NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"address/addressSelectList.api?access_token=%@",tokenStr];
    [HTTPURL postRequest:LINK_BASE_URL(urlStr) parameters:@{@"userId":userid} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        NSDictionary*dic=[PublicTools dictionaryWithJsonString:responseObject[@"data"]];
        dataArr=(NSArray*)dic;
        NSLog(@"%@",dic);
        [self.FavoritesTabelView reloadData];
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];
}
-(void)tianjiaDiZhi//添加
{
    NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"address/insertAddress.api?access_token=%@",tokenStr];
    UITextField*nameT=textFieldArr[0];
    UITextField*phoneT=textFieldArr[1];
    UITextField*dizhiT=textFieldArr[2];


    [HTTPURL postRequest:LINK_BASE_URL(urlStr) parameters:@{@"userId":userid,@"name":nameT.text,@"phone":phoneT.text,@"cityId":@"130100",@"areaId":@"130102",@"address":dizhiT.text,@"deleteFlag":@"2",@"isDefault":@"f"} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        [self chaxunList];
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self chaxunList];
    textFieldArr=[NSMutableArray new];
    self.title=@"管理收货地址";
    self.view.backgroundColor = COLOR(229, 230, 231, 1);
    [self addMyFavoritesTableView];//添加表
    // Do any additional setup after loading the view.
}
//
//添加表视图
-(void)addMyFavoritesTableView{
    //表格
    _FavoritesTabelView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, width_screen, height_screen-40-64) style:UITableViewStyleGrouped];
    _FavoritesTabelView.backgroundColor = [UIColor clearColor];
    _FavoritesTabelView.showsVerticalScrollIndicator = NO;//隐藏滑动条
    //    _tabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _FavoritesTabelView.separatorColor = COLOR(229, 230, 231, 1);//分割线颜色
    _FavoritesTabelView.delegate = self;
    _FavoritesTabelView.dataSource = self;
    [_FavoritesTabelView registerNib:[UINib nibWithNibName:@"LeeShippingAddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"OenCell"];
    [self.view addSubview:_FavoritesTabelView];
    
}
#pragma mark --tableView 代理方法
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 15)];
    view.backgroundColor = COLOR(229, 230, 231, 1);
    return view;
}
//多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArr.count;
}
//几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//区尾高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, width_screen, 50);
    [button setTitle:@"+ 新加收货地址" forState:UIControlStateNormal];
    button.tag = 10000;
    [button addTarget:self action:@selector(zengjiaBtnClicke:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:COLOR(0, 143, 207, 1) forState:UIControlStateNormal];
    [view addSubview:button];
    return view;
}
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LeeShippingAddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OenCell"];
    [cell.deleteBtn addTarget:self action:@selector(deleteButtonClick:) forControlEvents:64];
    [cell.morenBtn addTarget:self action:@selector(sheweimorenClick:) forControlEvents:64];
    cell.deleteBtn.tag=[dataArr[indexPath.row][@"id"]integerValue];
    cell.morenBtn.tag=[dataArr[indexPath.row][@"id"]integerValue];

    [cell.xiuGaiBtn addTarget:self action:@selector(zengjiaBtnClicke:) forControlEvents:UIControlEventTouchUpInside];
    cell.nameLabel.text=dataArr[indexPath.row][@"name"];
    cell.contenLabel.text=dataArr[indexPath.row][@"address"];
    cell.phonoLabel.text=[NSString stringWithFormat:@"%@",dataArr[indexPath.row][@"phone"]] ;
    return cell;
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}
//cell 分割线 两端封头 实现这个两个方法 1
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
#pragma mark---增加收货地址--修改收货地址方法
-(void)zengjiaBtnClicke:(UIButton *)button{
    if (button.tag==10000) {
         [self zengjiadizhissssss:NO];
    }else{
         [self zengjiadizhissssss:YES];
    }
 

}
//收货地址View
-(void)zengjiadizhissssss:(BOOL)xingming{
    
    _bigView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, height_screen)];
    _bigView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_bigView];
    
    UIButton *bigBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bigBtn.frame = CGRectMake(0, 0, width_screen, height_screen-324);
    [bigBtn addTarget:self action:@selector(xiaoshuBtnClcike) forControlEvents:UIControlEventTouchUpInside];
    [_bigView addSubview:bigBtn];
       UIView * _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, height_screen-324 , width_screen, 324)];
        _bgView.backgroundColor = [UIColor grayColor];
        [_bigView addSubview:_bgView];
        
        UIView *towviwe = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width_screen, 60)];
        towviwe.backgroundColor = [UIColor whiteColor];
        [_bgView addSubview:towviwe];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(width_screen-30, 10, 20, 20);
        button.backgroundColor =[UIColor redColor];
        [button addTarget:self action:@selector(xiaoshuBtnClcike) forControlEvents:UIControlEventTouchUpInside];
        [towviwe addSubview:button];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, width_screen/3*2, 40)];
        label.text = @"新增收货地址";
        label.font = [UIFont systemFontOfSize:20];
        [towviwe addSubview:label];
        NSArray *titleArr = @[@"收件人",@"联系电话",@"详细地址"];
        NSArray *sss = @[@"收件人姓名",@"收件人联系电话",@"详细地址门牌号"];
        for (int i=0; i<3; i++) {
            UIView *sview = [[UIView alloc]initWithFrame:CGRectMake(0, 61+(51*i), width_screen, 50)];
            sview.backgroundColor = [UIColor whiteColor];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 90, 40)];
            label.text = titleArr[i];
            
            UITextField *textfield = [[UITextField alloc]initWithFrame:CGRectMake(100, 10, width_screen-120, 40)];
            textfield.placeholder = sss[i];
            if (xingming) {
                textfield.text = @"王小明";
            }
            [sview addSubview:textfield];
            [textFieldArr addObject:textfield];
            [sview addSubview:label];
            [_bgView addSubview:sview];
            
        }
        UIView *lastView = [[UIView alloc]initWithFrame:CGRectMake(0, 214, width_screen, 50)];
        lastView.backgroundColor = [UIColor whiteColor];
        [_bgView addSubview:lastView];
        
        UIButton *morenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        morenBtn.frame = CGRectMake(10, 15, 20, 20);
        morenBtn.backgroundColor = [UIColor redColor];
        [lastView addSubview:morenBtn];
        
        UILabel *tishiLbale = [[UILabel alloc]initWithFrame:CGRectMake(40, 10, width_screen-100, 30)];
        tishiLbale.text = @"设为默认地址";
        [lastView addSubview:tishiLbale];
        
        UIButton *queDingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        queDingBtn.frame = CGRectMake(0, 264, width_screen, 60);
        queDingBtn.backgroundColor = [UIColor orangeColor];
        [queDingBtn setTitle:@"确 定" forState:UIControlStateNormal];
        [queDingBtn addTarget:self action:@selector(quedingbtnClicke) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:queDingBtn];
    
}
//取消地址
-(void)xiaoshuBtnClcike{
    [_bigView removeFromSuperview];
    _bigView.alpha=0.0;
}
//确定地址
-(void)quedingbtnClicke{
    [self tianjiaDiZhi];
    [_bigView removeFromSuperview];
    _bigView.alpha=0.0;
}
//删除地址
-(void)deleteButtonClick:(UIButton*)btn
{
    NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"address/deleteAddress.api?access_token=%@",tokenStr];
    
    NSString*dizhiId=[NSString stringWithFormat:@"%ld",(long)btn.tag];
    [HTTPURL postRequest:LINK_BASE_URL(urlStr) parameters:@{@"userId":userid,@"id":dizhiId} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        [self chaxunList];
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];

}
//设为默认
-(void)sheweimorenClick:(UIButton*)btn
{
    NSString*tokenStr= OBJECT_FOR_KEY(@"token");
    NSString*urlStr=[NSString stringWithFormat:@"address/updateDefault.api?access_token=%@",tokenStr];
    
    NSString*dizhiId=[NSString stringWithFormat:@"%ld",(long)btn.tag];
    [HTTPURL postRequest:LINK_BASE_URL(urlStr) parameters:@{@"userId":userid,@"id":dizhiId} success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@",responseObject);
        [self chaxunList];
    } filure:^(NSURLSessionDataTask *task, id error) {
        
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
